﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace ForexService
{
    public class ForexCityParser
    {
        string cityUrl = "http://www.forexfactory.com";

        //List<string> cities = new List<string>();

        List<ForexCity> cities = new List<ForexCity>();

        public List<ForexCity> CitiesList
        {
            get { return cities; }
            set { cities = value; }
        }

        public void ParseCityData()
        {
            cities.Clear();

            try
            {
                using (WebClient wc1 = new WebClient())
                {
                    byte[] dataBuffer = wc1.DownloadData(cityUrl);
                    
                    string download = Encoding.ASCII.GetString(dataBuffer);

                    //string today = GetDate(0, true);
                    //string tomorrow = GetDate(1, true);

                    //string[] rez = GetStringInBetween(today, tomorrow, download, true, true);

                    string regex = @"\t\t\t<span style=""color: #22229C; text-decoration: underline;"" onmouseover=""this.style.color = '#FF4400';"" onmouseout=""this.style.color = '#22229C';"">(.*)\</span>";

                    foreach (Match matchCity in Regex.Matches(download, regex))
                    {
                        string cityName = matchCity.Groups[1].Value;
                        if (cityName.Contains("</span>"))//write better reg exp to avoid this
                            cityName = cityName.Substring(0, cityName.IndexOf("</span>"));

                        cities.Add(new ForexCity(cityName));
                    }

                }
            }
            catch (Exception ex)
            {
               //Wrire log
            }
        }
    }
}
